package cn.toys.modular.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import cn.stylefeng.roses.kernel.model.response.ResponseData;
import cn.toys.base.auth.annotion.Permission;
import cn.toys.base.consts.CodeConstant;
import cn.toys.base.pojo.page.LayuiPageFactory;
import cn.toys.modular.entity.Product;
import cn.toys.modular.entity.ProductAttr;
import cn.toys.modular.model.CompanyDto;
import cn.toys.modular.service.ICompanyService;
import cn.toys.modular.warpper.ProductWrapper;
import cn.toys.sys.core.exception.enums.BizExceptionEnum;
import cn.toys.sys.core.log.LogObjectHolder;
import cn.toys.sys.modular.system.service.DeptService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 公司
 *
 * @author guoyu
 * @version 1.0
 * @date 2021/3/7 4:09 下午
 */
@Controller
@RequestMapping("/company")
public class CompanyController extends BaseController {

    private String PREFIX = "/modular/company/";

    @Autowired
    private ICompanyService companyService;

    /**
     * 跳转到产品管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "company.html";
    }

    /**
     * 跳转到添加产品管理
     */
    @RequestMapping("/company_add")
    public String companyAdd() {
        return PREFIX + "company_add.html";
    }

    /**
     * 跳转到修改产品管理
     */
    @RequestMapping("/company_edit")
    public String companyUpdate(@RequestParam Integer companyId) {
        CompanyDto companyDto = new CompanyDto();
        companyDto.setCompanyId(companyId);
        CompanyDto company = companyService.getCompany(companyDto);
        LogObjectHolder.me().set(company);
        return PREFIX + "company_edit.html";
    }


    @PostMapping(value = "/add")
    @ResponseBody
    public Object add(CompanyDto companyDto) {
        ResponseData responseData = checkParam(companyDto);
        if(responseData != null){
            return responseData;
        }
        Integer result = companyService.insert(companyDto);
        if(result != null && result != 1){
            return ResponseData.error(ResponseData.DEFAULT_ERROR_CODE, "保存失败");
        }
        return SUCCESS_TIP;
    }



    @PostMapping(value = "/edit")
    @ResponseBody
    public Object edit(CompanyDto companyDto) {
        if(companyDto.getCompanyId() != null){

        }
        ResponseData responseData = checkParam(companyDto);
        if(responseData != null){
            return responseData;
        }
        Integer result = companyService.update(companyDto);
        if(result != null && result != 1){
            return ResponseData.error(ResponseData.DEFAULT_ERROR_CODE, "保存失败");
        }
        return SUCCESS_TIP;
    }

    @PostMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestBody CompanyDto companyDto) {
        long total = companyService.getCompanyCount(companyDto);
        List<CompanyDto> list = companyService.list(companyDto);
        return LayuiPageFactory.createPageInfo(total, list);
    }

    /**
     * 产品类型详情
     */
    @RequestMapping(value = "/detail/{companyId}")
    @ResponseBody
    public Object detail(@PathVariable("companyId") Integer companyId) {
        if (ToolUtil.isEmpty(companyId)) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        CompanyDto companyDto = new CompanyDto();
        companyDto.setCompanyId(companyId);
        companyDto = companyService.getCompany(companyDto);
        Map<String, Object> roleMap = BeanUtil.beanToMap(companyDto);
        return ResponseData.success(roleMap);
    }

    //参数校验
    public ResponseData checkParam(CompanyDto companyDto){
        if(StringUtils.isEmpty(companyDto.getName())){
            return ResponseData.error(CodeConstant.PARAM_ERROR, "公司名称为空");
        }

        if(companyDto.getProvinceCode() == null){
            return ResponseData.error(CodeConstant.PARAM_ERROR, "省份code为空");
        }

        if(companyDto.getCityCode() == null){
            return ResponseData.error(CodeConstant.PARAM_ERROR, "城市code为空");
        }

        if(companyDto.getRegionCode() == null){
            return ResponseData.error(CodeConstant.PARAM_ERROR, "地区code为空");
        }

        if(StringUtils.isEmpty(companyDto.getAddress())){
            return ResponseData.error(CodeConstant.PARAM_ERROR, "详细地址为空");
        }

        if(StringUtils.isEmpty(companyDto.getLegalPerson())){
            return ResponseData.error(CodeConstant.PARAM_ERROR, "企业法人为空");
        }

        if(companyDto.getRegistMoney() == null){
            return ResponseData.error(CodeConstant.PARAM_ERROR, "注册资金为空");
        }


        if(StringUtils.isEmpty(companyDto.getEncompass())){
            return ResponseData.error(CodeConstant.PARAM_ERROR, "营业范围为空");
        }

        if(companyDto.getEstablishTime() == null){
            return ResponseData.error(CodeConstant.PARAM_ERROR, "成立时间为空");
        }
        return null;
    }


}
